import api from "../../api/mop";
import { err, get, to, toast } from "../../utils/wxapi";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 20,
    list: [],
    pageTotal: 0,
    role: get("user").role,
    userId: get("userId"),
  },
  onReady() {
    this.getlist();
  },
  async getlist() {
    try {
      const { page, list, size, role, userId } = this.data;
      let data = { page, size };
      if (role == 3) {
        data = {
          ...data,
          data: JSON.stringify({ userId }),
        };
      }
      const res = await api.work_list(data);
      console.log(res);
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        console.log(newlist);
        this.setData({
          pageTotal: res.pageTotal,
          list: list.length === 0 ? newlist : list.concat(newlist),
        });
      } else {
        err(res.msg);
      }
    } catch (error) {
      err(error);
    }
  },
  del(e) {
    const { id } = e.currentTarget.dataset;
    try {
      wx.showModal({
        title: "提示",
        content: "您确定删除该岗位吗？",
        complete: async (res) => {
          if (res.confirm) {
            const { respCode, msg } = await api.delwork({ id });
            if (respCode === 200) {
              toast("删除成功");
              this.setData({ page: 1, list: [] });
              this.getlist();
            } else {
              err(msg);
              toast(msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
    }
  },
  put(e) {
    const { id } = e.currentTarget.dataset;
    to("/two/pages/postAdd/postAdd?id=" + id);
  },
  onRecruits(e) {
    let { onrecruit, id } = e.currentTarget.dataset;
    console.log(e);

    if (onrecruit === 0) {
      onrecruit = 1;
    } else if (onrecruit === 1) {
      onrecruit = 0;
    }
    wx.showModal({
      title: "提示",
      content: "你确定将该岗位招聘状态？",
      complete: async (res) => {
        if (res.confirm) {
          try {
            const { respCode, msg } = await api.receuit({
              id,
              onRecruit: onrecruit,
            });
            if (respCode === 200) {
              toast("修改成功");
              this.setData({ page: 1, size: 20, list: [] });
              this.getlist();
            } else {
              err(msg);
            }
          } catch (error) {
            err(error);
          }
        }
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { page, list, pageTotal } = this.data;
    if (list.length >= pageTotal) {
      toast("已加载全部岗位");
    } else {
      this.setData({ page: page + 1 });
      this.getlist();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
